﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestTool
{
    class Program
    {
        const int maxPartDigit = 100;
        const int signatureLength = 6;
        const int maxPartCount = 5;
        const int minPartCount = 2;
        const string dataFileName = "data.txt";
        const string caseFileName = "cases.txt";
        static int partID = 1;

        private static void makeTestCase(string i_activeDir, int i_requestsNumber)
        {
            string casePath = System.IO.Path.Combine(i_activeDir, caseFileName);

            using (System.IO.StreamWriter fs = new System.IO.StreamWriter(casePath))
            {
                Random rnd = new Random(DateTime.Now.Millisecond);
                makeParts(fs, rnd, i_requestsNumber);
            }
        }

        private static void makeTestData(string i_activeDir, int i_aggregatesNumber)
        {
            string dataPath = System.IO.Path.Combine(i_activeDir, dataFileName);

            using (System.IO.StreamWriter fs = new System.IO.StreamWriter(dataPath))
            {
                Random rnd = new Random(DateTime.Now.Millisecond);

                for (int i = 1; i <= i_aggregatesNumber; ++i)
                {
                    fs.WriteLine(i);
                    makeParts(fs, rnd, rnd.Next(minPartCount, maxPartCount));
                }
            }
        }

        private static void makeParts(System.IO.StreamWriter i_fs, Random i_rnd, int i_numbers)
        {
            for (int j = 1; j <= i_numbers; ++j,++partID)
            {
                string str = Enumerable.Range(1, maxPartDigit).OrderBy(
                    (a) =>
                        i_rnd.Next(a, maxPartDigit)
                    ).Take(signatureLength)
                    .Aggregate<int, string>("",
                    (a, b) =>
                        (a == "") ? (partID + ": " + b) : (a + " " + b));
                i_fs.WriteLine(str);
            }
        }

        static void Main(string[] args)
        {
            string activeDir = args.Length > 1 ? args[1] : @"..\..\..\SearchTool\";
            
            // make test data
            makeTestData(activeDir, 50);

            // make test requests
            makeTestCase(activeDir, 20);
        }
    }
}
